package troqler;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.enterprise.context.SessionScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.sql.DataSource;

/**
 *
 */
@Named
@SessionScoped
public class OktTabell implements Serializable{
    private Oversikt oversikt;
    private List<TreningsOktBean> okter = Collections.synchronizedList(new ArrayList<TreningsOktBean>());
    private int sum = 0;
    private int maaned;
    private Integer aar = null;
    @Resource(name="jdbc/waplj_prosjekt")
    private DataSource ds;
    private Connection con = null;
    @Inject
    private BrukerBean bruker;
    
    public OktTabell()
    {
        
    }
    private void ryddOppDB()
    {
        if(con != null)
        {
            try {
            con.close();
            }
            catch(SQLException e)
            {
                
            }
            con = null;
        }
    }
    @PostConstruct
    public void postConstruct() throws SQLException
    {
        try {
            FacesContext cx = FacesContext.getCurrentInstance();
            Connection con = ds.getConnection();
            oversikt = new Oversikt(con,bruker.getBruker());
        } catch (SQLException ex) {
            
        }
        
        oppdater();
        ryddOppDB();
        
    }
    
    public List<TreningsOktBean> getTabellData()
    {
        return okter;
    }

    public synchronized int getMaaned() {
        return maaned;
    }

    public synchronized void setMaaned(int maaned) {
        this.maaned = maaned;
    }

    public Integer getAar() {
        return aar;
    }

    public void setAar(Integer aar) {
        this.aar = aar;
    }
    
    private synchronized void byggOkter(List<TreningsOkt> okter)
    {
        this.okter.clear();
        sum = 0;
        for(TreningsOkt o: okter)
        {
            this.okter.add(new TreningsOktBean(o));
            sum+=o.getVarighet();
        }
    }
    
    public synchronized void registrerNyOkt(TreningsOkt okt) throws SQLException
    {
        con = ds.getConnection();
        oversikt.registrerNyOkt(okt, con);
        okter.add(new TreningsOktBean(okt));
        this.sum +=okt.getVarighet();
        ryddOppDB();
    }
    public synchronized void oppdater() throws SQLException {
        if(con == null)
        {
            con = ds.getConnection();
            
        }
        
        
        for(int i = okter.size()-1;i >= 0;i--)
        {
            if(okter.get(i).getSlett())
            {
                oversikt.slettOkt(okter.get(i).getData(), con);
            }
            else if(okter.get(i).getEndret())
            {
                oversikt.oppdaterOkt(okter.get(i).getData(), con);
                okter.get(i).setEndret(false);
            }
        }
        if(maaned == 0 && aar == null)
        {
            byggOkter(oversikt.getAlleOkter());
        }
        else if(maaned >= 0 && maaned < 13)
        {
            byggOkter(oversikt.getOkterForMnd(aar, maaned-1));
        }
        ryddOppDB();
    }
    public int getSum()
    {
        return this.sum;
    }
    public int getAntall()
    {
        return okter.size();
    }
    public double getGjennomsnittVarighet() {
        if (this.getAntall() != 0) {
            return (double) this.getSum() / this.getAntall();
        } else {
            return 0;
        }
    }
}
